﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DTO;
using System.Data.OleDb;

namespace DAO
{
    public class KhachHangDAO: AbtractDAO
    {
        public static List<KhachHangDTO> LaydsKhachHang()
        {
            OleDbConnection ketNoi = null;
            List<KhachHangDTO> dsKhachHang = new List<KhachHangDTO>();
            try
            {
                ketNoi = KetNoi();
                string chuoiLenh = "SELECT MaKhachHang,TenKhachHang,KH.MaLoaiKhach,TenLoaiKhach,CMND,DiaChi FROM KHACHHANG KH, LOAIKHACHHANG LK WHERE KH.MaLoaiKhach=LK.MaLoaiKhach";
                OleDbCommand lenh = new OleDbCommand(chuoiLenh, ketNoi);

                OleDbDataReader boLoc = lenh.ExecuteReader();

                while (boLoc.Read())
                {
                    KhachHangDTO KhachHang = new KhachHangDTO();
                    KhachHang.MaKhachHang = boLoc.GetInt32(0);
                    if (!boLoc.IsDBNull(1))
                    {
                        KhachHang.TenKhachHang = boLoc.GetString(1);
                    }
                    LoaiKhachDTO lp = new LoaiKhachDTO();
                    lp.MaLoaiKhach = boLoc.GetInt32(2);
                    if (!boLoc.IsDBNull(3))
                    {
                        lp.TenLoaiKhach = boLoc.GetString(3);
                    }
                    KhachHang.LoaiKhach = lp;

                    if (!boLoc.IsDBNull(4))
                    {
                        KhachHang.SoCMND = boLoc.GetString(4);
                    }

                    if (!boLoc.IsDBNull(5))
                    {
                        KhachHang.DiaChi = boLoc.GetString(5);
                    }

                    dsKhachHang.Add(KhachHang);
                }
            }
            catch (Exception ex)
            {
                dsKhachHang = new List<KhachHangDTO>();
            }
            finally
            {
                if (ketNoi != null && ketNoi.State == System.Data.ConnectionState.Open)
                    ketNoi.Close();
            }
            return dsKhachHang;
        }
        public static List<KhachHangDTO> LaydsKHTheoMaPhongVaMaKH(string maKH,string maphong)
        {
            OleDbConnection ketNoi = null;
            List<KhachHangDTO> dsKhachHang = new List<KhachHangDTO>();
            try
            {
                ketNoi = KetNoi();
                string chuoiLenh = "SELECT KH.MaKhachHang,KH.TenKhachHang,KH.MaLoaiKhach,TenLoaiKhach,CMND,DiaChi FROM KHACHHANG KH, LOAIKHACHHANG LK,CHITIETPHIEUTHUE CTPT,PHIEUTHUE PT WHERE KH.MaLoaiKhach=LK.MaLoaiKhach and KH.MaKhachHang = CTPT.MaKhachHang and CTPT.MaPhieuThue = (select CTPT1.MaPhieuThue from CHITIETPHIEUTHUE CTPT1 where CTPT1.MaKhachHang = @MaKH) and PT.MaPhieuThue = CTPT.MaPhieuThue and PT.MaPhong = @MaPhong";
                OleDbCommand lenh = new OleDbCommand(chuoiLenh, ketNoi);
                OleDbParameter thamSo = new OleDbParameter("@MaKH", OleDbType.Char);
                thamSo.Value = maKH;
                lenh.Parameters.Add(thamSo);
                thamSo = new OleDbParameter("@MaPhong", OleDbType.Integer);
                thamSo.Value = Convert.ToInt32(maphong);

                lenh.Parameters.Add(thamSo);
                OleDbDataReader boLoc = lenh.ExecuteReader();

                while (boLoc.Read())
                {
                    KhachHangDTO KhachHang = new KhachHangDTO();
                    KhachHang.MaKhachHang = boLoc.GetInt32(0);
                    if (!boLoc.IsDBNull(1))
                    {
                        KhachHang.TenKhachHang = boLoc.GetString(1);
                    }
                    LoaiKhachDTO lp = new LoaiKhachDTO();
                    lp.MaLoaiKhach = boLoc.GetInt32(2);
                    if (!boLoc.IsDBNull(3))
                    {
                        lp.TenLoaiKhach = boLoc.GetString(3);
                    }
                    KhachHang.LoaiKhach = lp;

                    if (!boLoc.IsDBNull(4))
                    {
                        KhachHang.SoCMND = boLoc.GetString(4);
                    }

                    if (!boLoc.IsDBNull(5))
                    {
                        KhachHang.DiaChi = boLoc.GetString(5);
                    }

                    dsKhachHang.Add(KhachHang);
                }
            }
            catch (Exception ex)
            {
                dsKhachHang = new List<KhachHangDTO>();
            }
            finally
            {
                if (ketNoi != null && ketNoi.State == System.Data.ConnectionState.Open)
                    ketNoi.Close();
            }
            return dsKhachHang;
        }
        public static long layMaKHTheoTenVaCMND(string ten,string cmnd)
        {
            OleDbConnection ketNoi = null;
            long MaKH = 0;
            try
            {
                ketNoi = KetNoi();
                string chuoiLenh = "SELECT MaKhachHang FROM KHACHHANG KH WHERE KH.TenKhachHang= @TenKhachHang and KH.CMND = @CMND";
                OleDbCommand lenh = new OleDbCommand(chuoiLenh, ketNoi);
                OleDbParameter thamSo = new OleDbParameter("@TenKhachHang", OleDbType.VarChar);
                thamSo.Value = ten;
                lenh.Parameters.Add(thamSo);
                thamSo = new OleDbParameter("@CMND", OleDbType.VarChar);
                thamSo.Value = cmnd;              
                lenh.Parameters.Add(thamSo);
                OleDbDataReader boLoc = lenh.ExecuteReader();

                if (boLoc.Read())
                {
                    
                   MaKH = boLoc.GetInt32(0);
                   
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                if (ketNoi != null && ketNoi.State == System.Data.ConnectionState.Open)
                    ketNoi.Close();
            }
            return MaKH;
        }
        public static void themKhachHang(string tenKhachHang, long maLoaiKhach, string soCMND, string diaChi)
        {
            OleDbConnection ketNoi = null;
            try
            {
                ketNoi = KetNoi();
                string chuoiLenh = "INSERT INTO KHACHHANG(TenKhachHang,MaLoaiKhach,CMND,DiaChi) VALUES(@TenKhachHang,@MaLoaiKhach,@CMND,@DiaChi)";

                OleDbCommand lenh = new OleDbCommand(chuoiLenh, ketNoi);

                OleDbParameter thamSo;

                thamSo = new OleDbParameter("@TenKhachHang", OleDbType.VarChar);
                thamSo.Value = tenKhachHang;
                lenh.Parameters.Add(thamSo);

                thamSo = new OleDbParameter("@MaLoaiKhach", OleDbType.Integer);
                thamSo.Value = maLoaiKhach;
                lenh.Parameters.Add(thamSo);

                thamSo = new OleDbParameter("@CMND", OleDbType.VarChar);
                thamSo.Value = soCMND;
                lenh.Parameters.Add(thamSo);

                thamSo = new OleDbParameter("@DiaChi", OleDbType.VarChar);
                thamSo.Value = diaChi;
                lenh.Parameters.Add(thamSo);

                lenh.ExecuteNonQuery();
            }
            catch (Exception ex)
            {
                throw ex;
                
            }
            finally
            {
                if (ketNoi != null && ketNoi.State == System.Data.ConnectionState.Open)
                    ketNoi.Close();
            }
        }
        

    }
}
